package com.itheima.bos.service.take_delivery.impl;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.take_delivery.IWayBillRepository;
import com.itheima.bos.domain.take_delivery.WayBill;
import com.itheima.bos.index.WayBillIndexRepository;
import com.itheima.bos.service.take_delivery.IWayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements IWayBillService {

	@Autowired
	private IWayBillRepository wayBillRepository;

	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;

	// 保存运单
	@Override
	public void save(WayBill model) {
		// 判断运单号是否存在
		WayBill persistWayBill = wayBillRepository.findByWayBillNum(model
				.getWayBillNum());
		if (persistWayBill == null || persistWayBill.getId() == null) {
			// 运单不存在
			wayBillRepository.save(model);
			// 保存到搜索服务器中
			wayBillIndexRepository.save(model);

		} else {
			try {
				// 运单存在
				Integer id = persistWayBill.getId();
				BeanUtils.copyProperties(persistWayBill, model);
				persistWayBill.setId(id);
				// 保存到搜索服务器中
				wayBillIndexRepository.save(persistWayBill);
			} catch (BeansException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
	}

	// 分页查询
	@Override
	public Page<WayBill> pageQuery(WayBill model, Pageable pageable) {
		// 判断条件是否存在
		if (StringUtils.isBlank(model.getWayBillNum())
				&& StringUtils.isBlank(model.getSendAddress())
				&& StringUtils.isBlank(model.getRecAddress())
				&& StringUtils.isBlank(model.getSendProNum())
				&& (model.getSignStatus() == null || model.getSignStatus() == 0)) {
			// 无条件查询
			return wayBillRepository.findAll(pageable);
		} else {
			BoolQueryBuilder query = new BoolQueryBuilder();// 多条件组合查询
			// 向组合查询添加查询对象
			if (StringUtils.isNoneBlank(model.getWayBillNum())) {
				// 运单号查询
				QueryBuilder termBuilder = new TermQueryBuilder("wayBillNum",
						model.getWayBillNum());
				query.must(termBuilder);
			}
			if (StringUtils.isNoneBlank(model.getSendAddress())) {
				// 发货地模糊查询
				QueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder(
						"sendAddress", "*" + model.getSendAddress() + "*");
				query.must(wildcardQueryBuilder);
			}
			if (StringUtils.isNoneBlank(model.getRecAddress())) {
				QueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder(
						"recAddress", "*" + model.getRecAddress() + "*");
				query.must(wildcardQueryBuilder);
			}
			if (StringUtils.isNoneBlank(model.getSendProNum())) {
				//等值查询
				QueryBuilder termBuilder = new TermQueryBuilder("sendProNum",
						model.getSendProNum());
				query.must(termBuilder);
			}
			if (model.getSignStatus()!=null||model.getSignStatus()!=0) {
				//等值查询
				QueryBuilder termBuilder = new TermQueryBuilder("signStatus",
						model.getSignStatus());
				query.must(termBuilder);
			}
			SearchQuery searchQuery=new NativeSearchQuery(query);
			searchQuery.setPageable(pageable);
			return wayBillIndexRepository.search(searchQuery);
		}
	}

	// 根据运单号查询运单
	@Override
	public WayBill findByWayBillNum(String wayBillNum) {

		return wayBillRepository.findByWayBillNum(wayBillNum);
	}

}
